﻿using System;
using System.DHTML;
using Sys;
using Sys.UI;
using Sys.Mvc;

using JQuerySharp;
using VietPublisher.Scripts.Resources;

static public class SC_UserGroup
{
    public static string ControllerUrl = "UserGroup";

    public static void GetUserInGroup(int groupId, string groupCode)
    {
        Utils.SetInputValue("GroupId", groupId.ToString());
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/GetUsersOfGroup?groupid={0}", groupId);
        UIHelper.LoadAjax(href, "divUserInGroup", new MvcCallback(delegate(AjaxContext context)
            {
                Script.Literal("GridUserInGroup_onDataBound();");
            }), null);
    }


    public static void OpenAddNewUserToGroup()
    {
        string groupId = Utils.GetInputValue("GroupId");
        if (groupId == "" || groupId == "0")
        {
            Script.Alert(Common.SC_SelectUserGroupMsg);
            return;
        }

        SC_Popup.OpenUserDeptPopupMulti("");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="empCode"></param>
    /// <param name="empName"></param>
    public static void AddUserToGroup(string userId, string empCode, string empName)
    {
        string groupId = Utils.GetInputValue("GroupId");
        if (groupId == "" || groupId == "0")
        {
            Script.Alert(Common.SC_SelectUserGroupMsg);
            return;
        }
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/InsertUserInGroup?groupid={0}&empcode={1}", groupId, empCode);
        UIHelper.LoadAjaxNullEvent(href, "divUserInGroup", new Callback(delegate()
            {
                Script.Literal("GridUserInGroup_onDataBound();");
                Script.Alert(Common.OperationSuccess);
            }));
    }

    public static void RemoveUserFromGroup(TableCellElement cell)
    {
        if (!Script.Confirm(Common.DeleteConfirm))
        {
            return;
        }

        string groupId = Utils.GetInputValue("GroupId");
        string empCode = ((TableRowElement)cell.ParentNode).Cells[2].InnerText;
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/DeleteUserInGroup?groupid={0}&empCode={1}", groupId, empCode);
        UIHelper.LoadAjaxNullEvent(href, "divUserInGroup", new Callback(delegate()
        {
            Script.Literal("GridUserInGroup_onDataBound();");
            Script.Alert(Common.OperationSuccess);
        }));
    }
    
}
